#if defined lod_inc
 #endscript
#endif
#define lod_inc


#include <file>


#if !defined PATH_LOD
 #define PATH_LOD "data/lod2.dat"
#endif


/*
stock FindLODObject(modelid)
{
	new File:fp;

	fp = fopen("data/lod.dat", filemode:io_read);
	if(!fp) return 0;

	new line[256],
		tmp[64],
		index;

	while(fread(fp, line, sizeof(line), false) > 0)
	{
		if(line[0] == '#')
			continue;

		index = token_by_delim(line, tmp, ',', 0);
		if(index == -1) continue;

		if(strval(tmp) == modelid)
		{
			index = token_by_delim(line, tmp, ',', index+1);
			if(index == -1)
				continue;
			fclose(fp);
			return strval(tmp);
		}

	}
	fclose(fp);

	return modelid;

}*/

stock FindLODObject(modelid)
{
	new File:fp;

	fp = fopen(PATH_LOD, filemode:io_read);
	if(!fp) return modelid;

	new line[96],
		tmp[32],
		index,
		tmp2;

	while(fread(fp, line, sizeof(line), false) > 0)
	{
		if(line[0] == '#')
			continue;

		index = token_by_delim(line, tmp, ',', 0);
		if(index == -1) continue;

		tmp2 = strval(tmp);

		if(tmp2 == modelid) // found in the table
		{
			index = token_by_delim(line, tmp, ',', index+1);
			if(index == -1)
				continue;
			fclose(fp);
			return strval(tmp); // lod found
		}
		else if(modelid < tmp2) // ASC order
		{ // no LOD found
			fclose(fp);
			return modelid;
		}

	}
	fclose(fp);

	return modelid;

}











static stock token_by_delim(const string[], return_str[], delim, start_index)
{
	new x=0;
	while(string[start_index] == ' ' || string[start_index] == '\t')
		start_index++;
	while(string[start_index] != EOS && string[start_index] != delim && string[start_index] != '#')
	{
		return_str[x] = string[start_index];
		x++;
		start_index++;
	}
	return_str[x] = EOS;
	if(string[start_index] == EOS) start_index = (-1);
	return start_index;
}


